package com.shiro.demo.contrloller;

import com.shiro.demo.vo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
public class UserController {
    @RequestMapping(
            value="/subLogin",
            method=RequestMethod.POST,
            produces="application/json;charset=utf-8")
    @ResponseBody
    public String  sublogin(User user){
        Subject subject=SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(user.getUsername(),user.getPassword());
        System.out.println(user.getUsername()+user.getPassword());
        try{
            token.setRememberMe(user.getRememberMe());
            subject.login(token);
        }catch (AuthenticationException e){
           return e.getMessage();
        }
        if(subject.hasRole("admin")){
            return "youadmin";
        }
           return "wuadmin";
    }
    @ResponseBody
    @RequestMapping(value="/testRole",method = RequestMethod.GET)
    public String testRole(){
        return "testRole succes";
    }
    @ResponseBody
    @RequestMapping(value="/testRole1",method = RequestMethod.GET)
    public String testRole1(){
        return "testRole succes";
    }
    @ResponseBody
    @RequestMapping(value="/testPer",method = RequestMethod.GET)
    public String testPer(){
        return "testRolsafsde succes";
    }
    @ResponseBody
    @RequestMapping(value="/testPer1",method = RequestMethod.GET)
    public String testPer1(){
        return "testRoldsafe succes";
    }

    @RequestMapping(value="/test",method = RequestMethod.GET)
    public String test(){
        return "index";
    }
}
